package com.logos.reflect;

import com.logos.entity.Student;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MethodTest {

    public static void main(String[] args) throws Exception{
        // 获取Class对象
        Class<?> clazz = Class.forName("com.logos.entity.Student");
        // 获取所有方法
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
        // 获取单个方法
        Method eat = clazz.getDeclaredMethod("eat", String.class);
        Student stu = new Student("logos",19);
        // 暴力反射，取消访问检查
        eat.setAccessible(true);
        // 执行方法
        int result = (int) eat.invoke(stu, "bsJTB");
        System.out.println("result = " + result);
    }
}
